package com.wehealth.three.lib_connect.utils;


import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;


import com.wehealth.three.lib_connect.constant.CommonKeyConstant;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.ArrayList;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SaveLogUtil {
    public final static int LEADNUM = 12;
    public static void saveLeadBytes(String fileName, String content) {
        String name = "bleLeadDataByte" + fileName;
        BufferedWriter out = null;
        try {
            String filePath = CommonKeyConstant.ECG_DATA_REPORT + name;
            File file = new File(filePath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(filePath, true)));
            out.write(content);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void saveBytes(Context context, String fileName, String content) {
        String name = "bleDataByte_" + fileName + ".txt";
        BufferedWriter out = null;
        try {
            String filePath = context.getExternalFilesDir(CommonKeyConstant.Version_LOG) + File.separator + name;
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(filePath, true)));
            out.write(content);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void saveBleSN(String fileName, String content) {
        String name = "bleSN_int" + fileName + ".txt";
        BufferedWriter out = null;
        try {
            String filePath = CommonKeyConstant.ECG_DATA_REPORT + name;
            File file = new File(filePath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(filePath, true)));
            out.write(content);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void saveTempBytes(String fileName, String content) {
        String name = "bleTempDataByte" + fileName;
        BufferedWriter out = null;
        try {
            String filePath = CommonKeyConstant.ECG_DATA_REPORT + name;
            File file = new File(filePath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(filePath, true)));
            out.write(content);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void saveLogsNoSwitch1(String fileName, String content) {
        String name = "bleDataNoSwitch1" + fileName + ".txt";
        BufferedWriter out = null;
        try {
            String filePath = CommonKeyConstant.ECG_DATA_REPORT + name;
            File file = new File(filePath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(filePath, true)));
            out.write(content);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void saveLogsNoSwitch2(String fileName, String content) {
        String name = "bleDataNoSwitch2" + fileName + ".txt";
        BufferedWriter out = null;
        try {
            String filePath = CommonKeyConstant.ECG_DATA_REPORT + name;
            File file = new File(filePath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(filePath, true)));
            out.write(content);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void saveLogsNoSwitch3(String fileName, String content) {
        String name = "bleDataNoSwitch3" + fileName + ".txt";
        BufferedWriter out = null;
        try {
            String filePath = CommonKeyConstant.ECG_DATA_REPORT + name;
            File file = new File(filePath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(filePath, true)));
            out.write(content);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void saveLogsNoSwitch4(String fileName, String content) {
        String name = "bleDataNoSwitch4" + fileName + ".txt";
        BufferedWriter out = null;
        try {
            String filePath = CommonKeyConstant.ECG_DATA_REPORT + name;
            File file = new File(filePath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(filePath, true)));
            out.write(content);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void saveLogsNoSwitch5(String fileName, String content) {
        String name = "bleDataNoSwitch5" + fileName + ".txt";
        BufferedWriter out = null;
        try {
            String filePath = CommonKeyConstant.ECG_DATA_REPORT + name;
            File file = new File(filePath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(filePath, true)));
            out.write(content);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void saveLogsNoSwitch6(String fileName, String content) {
        String name = "bleDataNoSwitch6" + fileName + ".txt";
        BufferedWriter out = null;
        try {
            String filePath = CommonKeyConstant.ECG_DATA_REPORT + name;
            File file = new File(filePath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(filePath, true)));
            out.write(content);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void saveLogsNoSwitch7(String fileName, String content) {
        String name = "bleDataNoSwitch7" + fileName + ".txt";
        BufferedWriter out = null;
        try {
            String filePath = CommonKeyConstant.ECG_DATA_REPORT + name;
            File file = new File(filePath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(filePath, true)));
            out.write(content);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void saveLogsNoSwitch8(String fileName, String content) {
        String name = "bleDataNoSwitch8" + fileName + ".txt";
        BufferedWriter out = null;
        try {
            String filePath = CommonKeyConstant.ECG_DATA_REPORT + name;
            File file = new File(filePath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(filePath, true)));
            out.write(content);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void saveLogsSN(String fileName, String content) {
        String name = "bleDataSN" + fileName + ".txt";
        BufferedWriter out = null;
        try {
            String filePath = CommonKeyConstant.ECG_DATA_REPORT + name;
            File file = new File(filePath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(filePath, true)));
            out.write(content);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void saveLogs(String name, String content) {
        BufferedWriter out = null;
        try {
            String filePath = CommonKeyConstant.ECG_DATA_REPORT + name;
            File file = new File(filePath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(filePath, true)));
            out.write(content);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void saveLogsIsSwitch1(String fileName, String content) {
        String name = "bleDataIsSwitch1" + fileName + ".txt";
        BufferedWriter out = null;
        try {
            String filePath = CommonKeyConstant.ECG_DATA_REPORT + name;
            File file = new File(filePath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(filePath, true)));
            out.write(content);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void saveLogsIsSwitch2(String fileName, String content) {
        String name = "bleDataIsSwitch2" + fileName + ".txt";
        BufferedWriter out = null;
        try {
            String filePath = CommonKeyConstant.ECG_DATA_REPORT + name;
            File file = new File(filePath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(filePath, true)));
            out.write(content);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void saveLogsIsSwitch3(String fileName, String content) {
        String name = "bleDataIsSwitch3" + fileName + ".txt";
        BufferedWriter out = null;
        try {
            String filePath = CommonKeyConstant.ECG_DATA_REPORT + name;
            File file = new File(filePath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(filePath, true)));
            out.write(content);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void saveLogsIsSwitch4(String fileName, String content) {
        String name = "bleDataIsSwitch4" + fileName + ".txt";
        BufferedWriter out = null;
        try {
            String filePath = CommonKeyConstant.ECG_DATA_REPORT + name;
            File file = new File(filePath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(filePath, true)));
            out.write(content);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void saveLogsIsSwitch5(String fileName, String content) {
        String name = "bleDataIsSwitch5" + fileName + ".txt";
        BufferedWriter out = null;
        try {
            String filePath = CommonKeyConstant.ECG_DATA_REPORT + name;
            File file = new File(filePath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(filePath, true)));
            out.write(content);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void saveLogsIsSwitch6(String fileName, String content) {
        String name = "bleDataIsSwitch6" + fileName + ".txt";
        BufferedWriter out = null;
        try {
            String filePath = CommonKeyConstant.ECG_DATA_REPORT + name;
            File file = new File(filePath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(filePath, true)));
            out.write(content);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void saveLogsIsSwitch7(String fileName, String content) {
        String name = "bleDataIsSwitch7" + fileName + ".txt";
        BufferedWriter out = null;
        try {
            String filePath = CommonKeyConstant.ECG_DATA_REPORT + name;
            File file = new File(filePath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(filePath, true)));
            out.write(content);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void saveLogsIsSwitch8(String fileName, String content) {
        String name = "bleDataIsSwitch8" + fileName + ".txt";
        BufferedWriter out = null;
        try {
            String filePath = CommonKeyConstant.ECG_DATA_REPORT + name;
            File file = new File(filePath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(filePath, true)));
            out.write(content);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void saveLogsIsFilter1(String fileName, String content) {
        String name = "bleDataIsFilter1" + fileName + ".txt";
        BufferedWriter out = null;
        try {
            String filePath = CommonKeyConstant.ECG_DATA_REPORT + name;
            File file = new File(filePath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(filePath, true)));
            out.write(content);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void saveLogsChengQ0(String fileName, String content) {
        String name = "bleDataChengQ0" + fileName + ".txt";
        BufferedWriter out = null;
        try {
            String filePath = CommonKeyConstant.ECG_DATA_REPORT + name;
            File file = new File(filePath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(filePath, true)));
            out.write(content);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }



    public static String getSDPath() {
        File sdDir = null;
        boolean sdCardExist = Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
        if (sdCardExist) {
            sdDir = Environment.getExternalStorageDirectory();// 获取跟目录
        }
        return sdDir.toString();
    }



    /**
     * 将点的集合改为导的集合
     **/
    public static List<int[]> getEcgData(List<int[]> data) {
        List<int[]> des = new ArrayList<>();
        int[] d1 = new int[data.size()];
        int[] d2 = new int[data.size()];
        int[] d3 = new int[data.size()];
        int[] d4 = new int[data.size()];
        int[] d5 = new int[data.size()];
        int[] d6 = new int[data.size()];
        int[] d7 = new int[data.size()];
        int[] d8 = new int[data.size()];
        int[] d9 = new int[data.size()];
        int[] d10 = new int[data.size()];
        int[] d11 = new int[data.size()];
        int[] d12 = new int[data.size()];
        for (int j = 0; j < data.size(); j++) {
            for (int i = 0; i < LEADNUM; i++) {
                if (i == 0) {
                    d1[j] = data.get(j)[i];
                }
                if (i == 1) {
                    d2[j] = data.get(j)[i];
                }
                if (i == 2) {
                    d3[j] = data.get(j)[i];
                }
                if (i == 3) {
                    d4[j] = data.get(j)[i];
                }
                if (i == 4) {
                    d5[j] = data.get(j)[i];
                }
                if (i == 5) {
                    d6[j] = data.get(j)[i];
                }
                if (i == 6) {
                    d7[j] = data.get(j)[i];
                }
                if (i == 7) {
                    d8[j] = data.get(j)[i];
                }
                if (i == 8) {
                    d9[j] = data.get(j)[i];
                }
                if (i == 9) {
                    d10[j] = data.get(j)[i];
                }
                if (i == 10) {
                    d11[j] = data.get(j)[i];
                }
                if (i == 11) {
                    d12[j] = data.get(j)[i];
                }
            }
        }
        des.add(d1);
        des.add(d2);
        des.add(d3);
        des.add(d4);
        des.add(d5);
        des.add(d6);
        des.add(d7);
        des.add(d8);
        des.add(d9);
        des.add(d10);
        des.add(d11);
        des.add(d12);
        return des;
    }

    @SuppressLint("UseSparseArrays")
    public static Map<Integer, Integer[]> parseIntegerData(Map<String, Object> map) {
        Integer[] d0 = (Integer[]) map.get("ecgDataBuffer0");
        Integer[] d1 = (Integer[]) map.get("ecgDataBuffer1");
        Integer[] d2 = (Integer[]) map.get("ecgDataBuffer2");
        Integer[] d3 = (Integer[]) map.get("ecgDataBuffer3");
        Integer[] d4 = (Integer[]) map.get("ecgDataBuffer4");
        Integer[] d5 = (Integer[]) map.get("ecgDataBuffer5");
        Integer[] d6 = (Integer[]) map.get("ecgDataBuffer6");
        Integer[] d7 = (Integer[]) map.get("ecgDataBuffer7");
        Integer[] d8 = (Integer[]) map.get("ecgDataBuffer8");
        Integer[] d9 = (Integer[]) map.get("ecgDataBuffer9");
        Integer[] d10 = (Integer[]) map.get("ecgDataBuffer10");
        Integer[] d11 = (Integer[]) map.get("ecgDataBuffer11");

        Map<Integer, Integer[]> mapInt = new HashMap<Integer, Integer[]>();
        mapInt.put(0, d0);
        mapInt.put(1, d1);
        mapInt.put(2, d2);
        mapInt.put(3, d3);
        mapInt.put(4, d4);
        mapInt.put(5, d5);
        mapInt.put(6, d6);
        mapInt.put(7, d7);
        mapInt.put(8, d8);
        mapInt.put(9, d9);
        mapInt.put(10, d10);
        mapInt.put(11, d11);

        return mapInt;
    }

    public static int[] getMapEcgDataInts(Map<String, Object> map) {
        Integer[] d0 = (Integer[]) map.get("ecgDataBuffer0");
        Integer[] d1 = (Integer[]) map.get("ecgDataBuffer1");
        Integer[] d2 = (Integer[]) map.get("ecgDataBuffer2");
        Integer[] d3 = (Integer[]) map.get("ecgDataBuffer3");
        Integer[] d4 = (Integer[]) map.get("ecgDataBuffer4");
        Integer[] d5 = (Integer[]) map.get("ecgDataBuffer5");
        Integer[] d6 = (Integer[]) map.get("ecgDataBuffer6");
        Integer[] d7 = (Integer[]) map.get("ecgDataBuffer7");
        Integer[] d8 = (Integer[]) map.get("ecgDataBuffer8");
        Integer[] d9 = (Integer[]) map.get("ecgDataBuffer9");
        Integer[] d10 = (Integer[]) map.get("ecgDataBuffer10");
        Integer[] d11 = (Integer[]) map.get("ecgDataBuffer11");

        int[] data = new int[d0.length * 12];

        for (int i = 0; i < d0.length; i++) {//d0
            data[i] = d0[i];
        }
        for (int i = 0; i < d0.length; i++) {//d1
            data[d0.length + i] = d1[i];
        }
        for (int i = 0; i < d0.length; i++) {//d2
            data[d0.length * 2 + i] = d2[i];
        }
        for (int i = 0; i < d0.length; i++) {//d3
            data[d0.length * 3 + i] = d3[i];
        }
        for (int i = 0; i < d0.length; i++) {//d4
            data[d0.length * 4 + i] = d4[i];
        }
        for (int i = 0; i < d0.length; i++) {//d5
            data[d0.length * 5 + i] = d5[i];
        }
        for (int i = 0; i < d0.length; i++) {//d6
            data[d0.length * 6 + i] = d6[i];
        }
        for (int i = 0; i < d0.length; i++) {//d7
            data[d0.length * 7 + i] = d7[i];
        }
        for (int i = 0; i < d0.length; i++) {//d8
            data[d0.length * 8 + i] = d8[i];
        }
        for (int i = 0; i < d0.length; i++) {//d9
            data[d0.length * 9 + i] = d9[i];
        }
        for (int i = 0; i < d0.length; i++) {//d10
            data[d0.length * 10 + i] = d10[i];
        }
        for (int i = 0; i < d0.length; i++) {//d11
            data[d0.length * 11 + i] = d11[i];
        }
        return data;
    }

    @SuppressLint("UseSparseArrays")
    public static Map<Integer, int[]> parseIntData(List<int[]> bufferDrawSnap) {
        Map<Integer, int[]> map = new HashMap<>();
        int[] d1 = new int[bufferDrawSnap.size()];
        int[] d2 = new int[bufferDrawSnap.size()];
        int[] d3 = new int[bufferDrawSnap.size()];
        int[] d4 = new int[bufferDrawSnap.size()];
        int[] d5 = new int[bufferDrawSnap.size()];
        int[] d6 = new int[bufferDrawSnap.size()];
        int[] d7 = new int[bufferDrawSnap.size()];
        int[] d8 = new int[bufferDrawSnap.size()];
        int[] d9 = new int[bufferDrawSnap.size()];
        int[] d10 = new int[bufferDrawSnap.size()];
        int[] d11 = new int[bufferDrawSnap.size()];
        int[] d12 = new int[bufferDrawSnap.size()];
        for (int j = 0; j < bufferDrawSnap.size(); j++) {
            for (int i = 0; i < 12; i++) {
                if (i == 0) {
                    d1[j] = bufferDrawSnap.get(j)[i];
                }
                if (i == 1) {
                    d2[j] = bufferDrawSnap.get(j)[i];
                }
                if (i == 2) {
                    d3[j] = bufferDrawSnap.get(j)[i];
                }
                if (i == 3) {
                    d4[j] = bufferDrawSnap.get(j)[i];
                }
                if (i == 4) {
                    d5[j] = bufferDrawSnap.get(j)[i];
                }
                if (i == 5) {
                    d6[j] = bufferDrawSnap.get(j)[i];
                }
                if (i == 6) {
                    d7[j] = bufferDrawSnap.get(j)[i];
                }
                if (i == 7) {
                    d8[j] = bufferDrawSnap.get(j)[i];
                }
                if (i == 8) {
                    d9[j] = bufferDrawSnap.get(j)[i];
                }
                if (i == 9) {
                    d10[j] = bufferDrawSnap.get(j)[i];
                }
                if (i == 10) {
                    d11[j] = bufferDrawSnap.get(j)[i];
                }
                if (i == 11) {
                    d12[j] = bufferDrawSnap.get(j)[i];
                }
            }
        }
        map.put(0, d1);
        map.put(1, d2);
        map.put(2, d3);
        map.put(3, d4);
        map.put(4, d5);
        map.put(5, d6);
        map.put(6, d7);
        map.put(7, d8);
        map.put(8, d9);
        map.put(9, d10);
        map.put(10, d11);
        map.put(11, d12);
        return map;
    }

    public static String data2String(int[] data) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < data.length; i++) {
            sb.append(data[i] + " ");
        }
        return sb.toString();
    }

    public static Map<String, StringBuffer> parseIntDataToString(List<int[]> bufferDrawSnap) {
        Map<String, StringBuffer> map = new HashMap<String, StringBuffer>();
        StringBuffer sb1 = new StringBuffer();
        StringBuffer sb2 = new StringBuffer();
        StringBuffer sb3 = new StringBuffer();
        StringBuffer sb4 = new StringBuffer();
        StringBuffer sb5 = new StringBuffer();
        StringBuffer sb6 = new StringBuffer();
        StringBuffer sb7 = new StringBuffer();
        StringBuffer sb8 = new StringBuffer();
        StringBuffer sb9 = new StringBuffer();
        StringBuffer sb10 = new StringBuffer();
        StringBuffer sb11 = new StringBuffer();
        StringBuffer sb12 = new StringBuffer();
        for (int j = 0; j < bufferDrawSnap.size(); j++) {
            for (int i = 0; i < 12; i++) {
                if (i == 0) {
                    sb1.append(bufferDrawSnap.get(j)[i] + " ");
                }
                if (i == 1) {
                    sb2.append(bufferDrawSnap.get(j)[i] + " ");
                }
                if (i == 2) {
                    sb3.append(bufferDrawSnap.get(j)[i] + " ");
                }
                if (i == 3) {
                    sb4.append(bufferDrawSnap.get(j)[i] + " ");
                }
                if (i == 4) {
                    sb5.append(bufferDrawSnap.get(j)[i] + " ");
                }
                if (i == 5) {
                    sb6.append(bufferDrawSnap.get(j)[i] + " ");
                }
                if (i == 6) {
                    sb7.append(bufferDrawSnap.get(j)[i] + " ");
                }
                if (i == 7) {
                    sb8.append(bufferDrawSnap.get(j)[i] + " ");
                }
                if (i == 8) {
                    sb9.append(bufferDrawSnap.get(j)[i] + " ");
                }
                if (i == 9) {
                    sb10.append(bufferDrawSnap.get(j)[i] + " ");
                }
                if (i == 10) {
                    sb11.append(bufferDrawSnap.get(j)[i] + " ");
                }
                if (i == 11) {
                    sb12.append(bufferDrawSnap.get(j)[i] + " ");
                }
            }
        }
        map.put("Ch0", sb1);
        map.put("Ch1", sb2);
        map.put("Ch2", sb3);
        map.put("Ch3", sb4);
        map.put("Ch4", sb5);
        map.put("Ch5", sb6);
        map.put("Ch6", sb7);
        map.put("Ch7", sb8);
        map.put("Ch8", sb9);
        map.put("Ch9", sb10);
        map.put("Ch10", sb11);
        map.put("Ch11", sb12);
        return map;
    }

    public static String parseJson(String string) {
        String result = "";
        if (TextUtils.isEmpty(string)) {
            return result;
        }
        try {
            JSONObject jsonObject = new JSONObject(string);
            result = jsonObject.getString("userName");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String parseNewGroupJson(String json) {
        String result = "";
        if (TextUtils.isEmpty(json)) {
            return result;
        }
        try {
            JSONObject jsonObject = new JSONObject(json);
            String new_group = jsonObject.getString("action");
            if (new_group.equals("new_group")) {
                result = jsonObject.getString("groupId");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String parseAction(String json) {
        String action = "";
        if (TextUtils.isEmpty(json)) {
            return action;
        }
        try {
            JSONObject jsonObject = new JSONObject(json);
            action = jsonObject.getString("action");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return action;
    }


    /**
     * 获取文件扩展名
     *
     * @param fileName
     * @return
     */
    public static String getFileFormat(String fileName) {
        if (SaveLogUtil.isEmpty(fileName))
            return "";

        int point = fileName.lastIndexOf('.');
        return fileName.substring(point + 1);
    }


    /**
     * 改变字符编码
     *
     * @param str 目标字符
     * @return 转换字符
     */
    public static String changeEncoding(String str, String encoding) {
        try {
            String returnStr = new String(str.getBytes(), encoding);
            return returnStr;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return str;
    }

    /**
     * 判断给定字符串是否空白串。 空白串是指由空格、制表符、回车符、换行符组成的字符串 若输入字符串为null或空字符串，返回true
     *
     * @param input
     * @return boolean
     */
    public static boolean isEmpty(String input) {
        if (input == null || "".equals(input))
            return true;

        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
                return false;
            }
        }
        return true;
    }




    public static void writeException(Exception e, String type) {
        StringBuffer sb = new StringBuffer();

        Writer writer = new StringWriter();
        PrintWriter printWriter = new PrintWriter(writer);
        Throwable cause = e.getCause();
        while (cause != null) {
            cause.printStackTrace(printWriter);
            cause = cause.getCause();
        }
        printWriter.close();
        String result = writer.toString();
        sb.append(result);
        String fileName = getSDPath() + "/" + "exception_ios.txt";
        writException2File(fileName, "\n \n" + type + "+++++" + sb.toString() + "\n \n");
    }

    /**
     * 追加文件：使用FileOutputStream，在构造FileOutputStream时，把第二个参数设为true
     *
     * @param fileName
     * @param content
     */
    public static void writException2File(String fileName, String content) {
        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(fileName, true)));
            out.write(content);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 追加文件：使用FileOutputStream，在构造FileOutputStream时，把第二个参数设为true
     *
     * @param fileName 文件名
     * @param content  log内容
     */
    public static void writLog2File(String fileName, String content) {
        BufferedWriter out = null;
        try {
//            String filePath = getSDPath() + "/ECGDATA/logs/" + fileName;
            String filePath = AppGlobals.INSTANCE.get().getExternalFilesDir(CommonKeyConstant.Version_LOG) + File.separator + fileName;
            File file = new File(filePath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(filePath, true)));
            out.write(content + "\n");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
